window.onload = function() {
    var nav = document.getElementById('nav');
    var brand = document.getElementById('brand');
    getAttr(nav.value);
    getBrand(nav.value);
    nav.onchange = function() {
    	//如果选项不是"请选择一个选项的话"，则不需要ajax改变品牌和属性
    	if(this.value != -1) {
    	   getAttr2(this.value);	
    	   getBrand(this.value);
    	} else {
    		brand.options.length = 1; 
    	}
    }
}

function getAttr(id) {
    ajax({
        'method':'get',
        'async':false,
        'url':'?a=goods&m=getAttr&id='+id,
        success:function(text) {
            if(text !== '') {
                var attrArr = text.split(';');
                var alldd = document.getElementsByTagName('dd');
                var attrId = document.getElementById('attrid');
                //从后面开始删除才能删除完整
                for(var i=alldd.length-1;i>=0;i--) {
                    if(alldd[i].className == 'temp') {
                        alldd[i].parentNode.removeChild(alldd[i]);
                    }
                }
                var ddRe = document.getElementsByTagName('dd')[4];
                for(var i=0;i<attrArr.length;i++) {
                    var attrName = attrArr[i].substr(0,attrArr[i].indexOf(':'));
                    var attrPrototype = attrName;
                    if(attrName.length == 2) {
                        attrName = attrName.charAt(0) + '　　' + attrName.charAt(1)+'：';
                    } else if(attrName.length == 3) {
                        attrName = attrName.charAt(0) + ' ' + attrName.charAt(1) + ' ' + attrName.charAt(2)+'：';
                    }
                    
                    var attrValueArr = attrArr[i].substr(attrArr[i].indexOf(':')+1).split('|');                   
                    var dd = document.createElement('dd');
                    var name = document.createTextNode(attrName);
                    dd.className = 'temp';
                    dd.appendChild(name);
                     for(var j=0;j<attrValueArr.length;j++) {                   
                        var input = document.createElement('input');
                        var text = document.createTextNode(attrValueArr[j]+' ');
                        input.appendChild(text);
                        input.type = 'checkbox';
                        input.name = 'attr['+attrPrototype+'][]';
                        input.value = attrValueArr[j];
                        var tArr = attrId.value.split(';');
                        for(var k=0;k<tArr.length;k++) {
                            var tempAttrArr = tArr[k].substr(tArr[k].indexOf(':')+1).split('|');
                            for(var l=0;l<tempAttrArr.length;l++) {
                            	if(tempAttrArr[l] == input.value) {
                            		input.checked = true;
                            	} 
                            }
                        }
                        dd.appendChild(input);
                        dd.appendChild(text);
                    }
                    ddRe.parentNode.insertBefore(dd,ddRe);
                } 
            }
        }
    });
}

function getAttr2(id) {  
    ajax({
        'method':'get',
        'async':false,
        'url':'?a=goods&m=getAttr&id='+id,
        success:function(text) {
            if(text !== '') {
                var attrArr = text.split(';');
                var alldd = document.getElementsByTagName('dd');
                //从后面开始删除才能删除完整
                for(var i=alldd.length-1;i>=0;i--) {
                    if(alldd[i].className == 'temp') {
                        alldd[i].parentNode.removeChild(alldd[i]);
                    }
                }
                var ddRe = document.getElementsByTagName('dd')[4];
                for(var i=0;i<attrArr.length;i++) {
                    var attrName = attrArr[i].substr(0,attrArr[i].indexOf(':'));
                    var attrPrototype = attrName;
                    if(attrName.length == 2) {
                        attrName = attrName.charAt(0) + '　　' + attrName.charAt(1)+'：';
                    } else if(attrName.length == 3) {
                        attrName = attrName.charAt(0) + ' ' + attrName.charAt(1) + ' ' + attrName.charAt(2)+'：';
                    }
                    
                    var attrValueArr = attrArr[i].substr(attrArr[i].indexOf(':')+1).split('|');                   
                    var dd = document.createElement('dd');
                    var name = document.createTextNode(attrName);
                    dd.className = 'temp';
                    dd.appendChild(name);
                     for(var j=0;j<attrValueArr.length;j++) {                   
                        var input = document.createElement('input');
                        var text = document.createTextNode(attrValueArr[j]+' ');
                        input.appendChild(text);
                        input.type = 'checkbox';
                        input.checked = true;
                        input.name = 'attr['+attrPrototype+'][]';
                        input.value = attrValueArr[j];
                        dd.appendChild(input);
                        dd.appendChild(text);
                    }
                    ddRe.parentNode.insertBefore(dd,ddRe);
                } 
            } else {
                var alldd = document.getElementsByTagName('dd');
                //从后面开始删除才能删除完整
                for(var i=alldd.length-1;i>=0;i--) {
                    if(alldd[i].className == 'temp') {
                        alldd[i].parentNode.removeChild(alldd[i]);
                    }
                }
            }
        }
    });
}

function checkUpdateGoods() { 
    var fm = document.add;
    if(fm.nav.value == -1) {
        alert('请务必选择一个商品类型!');
        fm.nav.focus();
        return false;
    }
    if(fm.brand.value == -1) {
        alert('请务必选择一个商品品牌!');
        fm.brand.focus();
        return false;
    }
    if(fm.name.value == '') {
        alert('商品名称不能为空!');
        fm.name.focus();
        return false;
    }
    if(fm.name.value.length < 2) {
        alert('商品名称不能少于两位!');
        fm.name.focus();
        return false;
    }
    if(fm.name.value.length > 100) {
        alert('商品名称不能大于一百位!');
        fm.name.focus();
        return false;
    }
    if(fm.sn.value.length < 2) {
        alert('商品编号不能少于两个字符!');
        fm.sn.focus();
        return false;
    }
    if(fm.sn.value.length > 50) {
        alert('商品编号不能大于五十个字符!');
        fm.sn.focus();
        return false;
    }
    return true;
}

function getBrand(id) {
    var brand = document.getElementById('brand');
    var brandid = document.getElementById('brandid');
    ajax({
    	'method':'get',
    	'async':false,
    	'url':'?a=goods&m=getBrand&id='+id,
    	success:function(text) {
    	   var a = text.split(':');
    	   brand.options.length = 1;       //初始化商品品牌选项，以免累加
    	   for(var i=0;i<a.length;i=i+2) {
    	       if(brandid.value == a[i]) {
    	           //brand.options.add(new Option(a[i+1],a[i],'selected'));    //按照options的构造函数的格式 value | text(次方法不兼容chrome和ie浏览器)
    	           var op = document.createElement('option');
    	           op.value = a[i];
    	           op.text = a[i+1];
    	           op.selected = true;
    	       } else {
    	       	   var op = document.createElement('option');
                   op.value = a[i];
                   op.text = a[i+1];
    	       }
    	       brand.options.add(op);
    	   }
    	}
    });
}
function centerWindow(url,name,width,height) {
    var top = (screen.height - height) / 2 - 50;
    var left = (screen.width - width) / 2;
    window.open(url,name,'width='+width+',height='+height+',top='+top+',left='+left);
}